﻿//
// Copyright (c) 2008-2010 Wouter Demuynck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Globalization;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BuildTools.UnitTests
{
    [TestClass]
    public class VersionNumberGeneratorTests
    {
        [TestInitialize]
        public void Initialize()
        {
            if (File.Exists("versionFile.version"))
                File.Delete("versionFile.version");
        }

        [TestMethod]
        public void IncrementTest()
        {
            Version version1 = VersionNumberGenerator.GenerateVersion("versionFile.version", BuildNumberType.Increment, RevisionNumberType.Increment);
            Assert.AreEqual(new Version(1, 0, 0, 0), version1);

            Version version2 = VersionNumberGenerator.GenerateVersion("versionFile.version", BuildNumberType.Increment, RevisionNumberType.Increment);
            Assert.AreEqual(new Version(1, 0, 1, 1), version2);

            Version version3 = VersionNumberGenerator.GenerateVersion("versionFile.version", BuildNumberType.Fixed, RevisionNumberType.Increment);
            Assert.AreEqual(new Version(1, 0, 1, 2), version3);

            Version version4 = VersionNumberGenerator.GenerateVersion("versionFile.version", BuildNumberType.Fixed, RevisionNumberType.BuildIncrement);
            Assert.AreEqual(new Version(1, 0, 1, 3), version4);

            Version version5 = VersionNumberGenerator.GenerateVersion("versionFile.version", BuildNumberType.Increment, RevisionNumberType.BuildIncrement);
            Assert.AreEqual(new Version(1, 0, 2, 0), version5);

            Version version6 = VersionNumberGenerator.GenerateVersion("versionFile.version", BuildNumberType.YearMonthDay, RevisionNumberType.HourMinute);
            Assert.AreEqual(new Version(1, 0, int.Parse(DateTime.UtcNow.ToString("MMdd", CultureInfo.InvariantCulture)), int.Parse(DateTime.UtcNow.ToString("HHmm", CultureInfo.InvariantCulture))), version6);
        }

        [TestMethod]
        public void YearMonthDayTest()
        {
            Version version1 = VersionNumberGenerator.GenerateVersion(new Version(2, 1), BuildNumberType.YearMonthDay, RevisionNumberType.HourMinute, DateTime.UtcNow.Subtract(TimeSpan.FromDays(370)));
            Assert.AreEqual(new Version(2, 1, int.Parse(DateTime.UtcNow.ToString("1MMdd", CultureInfo.InvariantCulture)), int.Parse(DateTime.UtcNow.ToString("HHmm", CultureInfo.InvariantCulture))), version1);

            // Test whether the overflow protection mechanism works.
            Version version2 = VersionNumberGenerator.GenerateVersion(new Version(2, 1), BuildNumberType.YearMonthDay, RevisionNumberType.HourMinute, DateTime.UtcNow.Subtract(TimeSpan.FromDays(2560)));
            Assert.AreEqual(new Version(2, 1, int.Parse(DateTime.UtcNow.ToString("MMdd", CultureInfo.InvariantCulture)), int.Parse(DateTime.UtcNow.ToString("HHmm", CultureInfo.InvariantCulture))), version2);
        }
    }
}
